package com.example.demo.controller.role;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.role.Role;
import com.example.demo.service.role.RoleService;
import com.example.demo.web.TableDataInfo;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
	@Autowired
	private RoleService roleService;

	@GetMapping()
	public String role() {
		return "role/role";
	}

	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Role role) {
		startPage();
		List<Role> list = roleService.selectRoleList(role);
		return getDataTable(list);
	}

	/**
	 * 新增角色
	 */
	@GetMapping("/add")
	public String add() {
		return "role/add";
	}

	@PostMapping("/add")
	@Transactional(rollbackFor = Exception.class)
	@ResponseBody
	public AjaxResult addSave(Role role) {
		return toAjax(roleService.insert(role));

	}

	@GetMapping("/edit/{roleId}")
	public String edit(@PathVariable("roleId") Integer roleId, ModelMap mmap) {
		mmap.put("role", roleService.selectByPrimaryKey(roleId));
		return "role/edit";
	}

	/**
	 * 新增数据权限
	 */
	@PostMapping("/edit")
	@Transactional(rollbackFor = Exception.class)
	@ResponseBody
	public AjaxResult editSave(Role role) {
		System.out.println(role.getStatus());
		return toAjax(roleService.updateByPrimaryKeySelective(role));
	}

	

	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		String[] id = ids.split(",");
		int flag = 0;
		for (int i = 0; i < id.length; i++) {
			Integer roleId = Integer.parseInt(id[i]);
			if (roleService.countUserRoleByRoleId(roleId) > 0) {
				flag = 1;
				return error(1, "所选的角色已被使用,不允许删除！");
			}
			if (flag == 1) {
				break;
			}
		}
		return toAjax(roleService.deleteRoleByIds(id));

	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree")
	public String selectMenuTree() {
		return "role/tree";
	}

	/**
	 * 校验角色名称
	 */
	@PostMapping("/checkRoleNameUnique")
	@ResponseBody
	public int checkRoleNameUnique(Role role) {
		
		
		return roleService.checkRoleNameUnique(role);
	}

	@PostMapping("/checkRoleKeyUnique")
	@ResponseBody
	public String checkRoleKeyUnique(Role role) {

		Integer id = role.getRoleId() == null ? -1 : role.getRoleId();
		Role r = roleService.checkRoleKeyUnique(role);
		if (r != null && r.getRoleId() != id)
			return "1";
		return "0";

	}

	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(Role role) {
		return toAjax(roleService.changeStatus(role));
	}

}
